<?php
class RegionApp extends MemberbaseApp
{
	var $_region_mod;
	
	function __construct(){
		$this->RegionApp();
	}
	
	function RegionApp(){
		parent::__construct();
		$this->_region_mod=&m('region');
	}
	
	
	//地区管理
	function index(){
		/* 取得地区 */
		$regions = $this->_region_mod->get_list(0);
		foreach ($regions as $key => $val)
		{
			$regions[$key]['switchs'] = 0;
			if ($this->_region_mod->get_list($val['region_id']))
			{
				$regions[$key]['switchs'] = 1;
			}
		}
		$this->assign('regions', $regions);
		$this->import_resource(array('script' => 'inline_edit.js,jqtreetable.js',
				'style' => 'res:style/jqtreetable.css'));
		$this->_curlocal(Lang::get('regions_manage'));
		$this->display('region.index.html');
	}
	
	/* 新增 */
	function add()
	{
		if (!IS_POST)
		{
			/* 参数 */
			$pid = empty($_GET['pid']) ? 0 : intval($_GET['pid']);
			$region = array('parent_id' => $pid, 'sort_order' => 255);
			$this->assign('region', $region);
	
			$this->assign('parents', $this->_get_options());
			$this->display('region.form.html');
		}
		else
		{
			$data = array(
					'region_name' => $_POST['region_name'],
					'parent_id' => $_POST['parent_id'],
					'sort_order' => $_POST['sort_order'],
			);
	
			/* 检查名称是否已存在 */
			if (!$this->_region_mod->unique(trim($data['region_name']), $data['parent_id']))
			{
				$this->show_warning('name_exist');
				return;
			}
	
			/* 保存 */
			$region_id = $this->_region_mod->add($data);
			if (!$region_id)
			{
				$this->show_warning($this->_region_mod->get_error());
				return;
			}
	
			$this->show_message('add_ok',
					'back_list',    'index.php?app=region',
					'continue_add', 'index.php?app=region&amp;act=add&amp;pid=' . $data['parent_id']
			);
		}
	}
	
	/* 编辑 */
	function edit()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!IS_POST)
		{
			/* 是否存在 */
			$region = $this->_region_mod->get_info($id);
			if (!$region)
			{
				$this->show_warning('region_empty');
				return;
			}
			$this->assign('region', $region);
	
			$this->assign('parents', $this->_get_options($id));
			$this->display('region.form.html');
		}
		else
		{
			$data = array(
					'region_name' => $_POST['region_name'],
					'parent_id'   => $_POST['parent_id'],
					'sort_order'  => $_POST['sort_order'],
			);
			
			/* 检查名称是否已存在 */
			if (!$this->_region_mod->unique(trim($data['region_name']), $data['parent_id'], $id))
			{
				$this->show_warning('name_exist');
				return;
			}
	
			/* 保存 */
			$rows = $this->_region_mod->edit($id, $data);
			if ($this->_region_mod->has_error())
			{
				$this->show_warning($this->_region_mod->get_error());
				return;
			}
	
			$this->show_message('edit_ok',
					'back_list',    'index.php?app=region',
					'edit_again',   'index.php?app=region&amp;act=edit&amp;id=' . $id
			);
		}
	}
	
	
	/* 异步取下一级地区  */
	function ajax_cate()
	{
		if(!isset($_GET['id']) || empty($_GET['id']))
		{
			echo ecm_json_encode(false);
			return;
		}
		$cate = $this->_region_mod->get_list($_GET['id']);
		foreach ($cate as $key => $val)
		{
			$child = $this->_region_mod->get_list($val['region_id']);
			if (!$child || empty($child))
			{
				$cate[$key]['switchs'] = 0;
			}
			else
			{
				$cate[$key]['switchs'] = 1;
			}
		}
		header("Content-Type:text/html;charset=" . CHARSET);
		echo ecm_json_encode(array_values($cate));
		//$this->json_result($cate);
		return ;
	}
	
	/* 删除 */
	function drop()
	{
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$id)
		{
			$this->show_warning('no_region_to_drop');
			return;
		}
		$ids = explode(',', $id);
		if (!$this->_region_mod->drop($ids))
		{
			$this->show_warning($this->_region_mod->get_error());
			return;
		}
	
		$this->show_message('drop_ok');
	}
	
	/* 构造并返回树 */
	function &_tree($regions)
	{
		import('tree.lib');
		$tree = new Tree();
		$tree->setTree($regions, 'region_id', 'parent_id', 'region_name');
		return $tree;
	}
	
	/* 取得可以作为上级的地区数据 */
	function _get_options($except = NULL)
	{
		$regions = $this->_region_mod->get_list();
		$tree =& $this->_tree($regions);
		return $tree->getOptions(MAX_LAYER - 1, 0, $except);
	}
}